#!/bin/bash

################################################################################################
# POP-C++ Release Archiver
# Author : Clement Valentin (clementval)
# Creation date : 06/04/2011
# Description : This script is used to create a releasable archive of POP-C++
# ----------------------------------------------------------------------------------------------
# IMPORTANT : Please, keep this log up to date
#
# Update log
# Date          Author          Description
# 31/03/2011    clementval      First version of this script
# 05/06/2012    clementval      Update script to have proper release without any dev folder or files
# 04/09/2014    baptistewicht   Cleanup and fix the script for 3.0
################################################################################################


################################################################
# Define the function to print the program usage
################################################################
usage()
{
cat << EOF

POP-C++ releaser script v1.0

This program is used to create a releasable archive of POP-C++

Usage: create_release <version>
       - version should be in the form of 1.0, 2.0, 2.5.1 ...
EOF
}

if [ "x$1" == "x" ]
then
   echo "[ERROR] You must specify a version number"
   usage
   exit
fi

echo "IMPORTANT FOR THE RELEASE"
echo "All non-versioned files will be DELETED!"
echo "Did you update the following files:"
echo "- RELEASE_NOTES"

read -p "If everything is fine just proceed with the archive ! (y/n) " -n 1 -r
echo    # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
    # Proceed with the release

    #Clean the current version of POP-C++
    git clean -xdf

    #Remove UDS files
    find . -xdev -type s -name ".uds_*" | xargs rm

    VERSION=$1
    #Date of the arhiving
    DDATE=$(date +%Y%m%d)
    RELDIR="popcpp_"$VERSION"_build_"$DDATE
    ARCHIVE="$RELDIR.tar"

    mkdir $RELDIR
    GOODDIR=$(ls . | grep -v dev | grep -v ReleaseNotes.dev.txt | grep -v config.status)

    echo $GOODDIR

    for d in $GOODDIR
    do
        if [ "x$d" != "x$RELDIR" ]
        then
            cp -R $d $RELDIR
            echo "cp -R $d $RELDIR"
        fi
    done


    tar -cf $ARCHIVE --exclude='.git' $RELDIR/*
    gzip $ARCHIVE
    rm -rf $RELDIR
fi
